#include <stdio.h>
#include <stdlib.h>
#define SIZE 100 //最大队列长度

typedef struct
{
    int *base; //初始化动态分配空间
    int front;
    int rear;
} SqQueue;

void InitQueue(SqQueue &Q)
{
    //构造一个空队列
    Q.base = (int *)malloc(SIZE * sizeof(int));
    if (!Q.base)
        exit(0);
    else
        Q.front = Q.rear = 0;
}

int QueueLength(SqQueue Q)
{
    //求队列的长度
    return (Q.rear - Q.front + SIZE) % SIZE;
}

int QueueEmpty(SqQueue Q)
{
    //判空
    if (Q.rear == Q.front)
        return 1;
    else
        return 0;
}

void EnQueue(SqQueue &Q, int e)
{
    //插入元素e为Q的新的队尾元素
    if ((Q.rear + 1) % SIZE == Q.front)
        exit(0);
    else
        Q.base[Q.rear] = e;
    Q.rear = (Q.rear + 1) % SIZE;
}

void DeQueue(SqQueue &Q, int &e)
{
    // 删除Q的队头元素, 并用e返回其值
    if ((Q.rear + 1) % SIZE == Q.front)
        exit(0);
    else
        e = Q.base[Q.front];
    Q.front = (Q.front + 1) % SIZE;
}

void GetHead(SqQueue Q, int &e)
{
    // 取Q的队头元素, 并用e返回其值
    if ((Q.rear + 1) % SIZE == Q.front)
        exit(0);
    else
        e = Q.base[Q.front];
}

void DancingPartner(SqQueue &M, SqQueue &W)
{
    //交流配对
    int i, j, n, min, len1, len2, man, woman, first;
    len1 = QueueLength(M);
    len2 = QueueLength(W);
    min = len1 < len2 ? len1 : len2;
    printf("请输入交流的轮数：");
    scanf("%d", &n);
    for (i = 1; i <= n; i++)
    {
        printf("第%d组配对交流：\n", i);
        for (j = 0; j < min; j++)
        {
            DeQueue(M, man);
            DeQueue(W, woman);
            printf("<%d, %d>\n", man, woman);
            EnQueue(M, man);
            EnQueue(W, woman);
        }
        if (len1 > len2)
            GetHead(M, first);
        else
            GetHead(W, first);
        printf("下一个轮讨论首人是：%d\n", first);
    }
}

int main()
{
    int name;
    SqQueue M, W;
    InitQueue(M);
    InitQueue(W);
    printf("男生：(0表示结束)\n");
    scanf("%d", &name);
    while (name)
    {
        EnQueue(M, name);
        scanf("%d", &name);
    }
    printf("女生：(0表示结束)\n");
    scanf("%d", &name);
    while (name)
    {
        EnQueue(W, name);
        scanf("%d", &name);
    }

    DancingPartner(M, W);

    return 0;
}